package com.xudow.app.dynamicstate_old.module.follow;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;

import com.jude.beam.expansion.list.BeamListActivityPresenter;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.xudow.app.dynamicstate_old.data.DynamicModel;
import com.xudow.app.dynamicstate_old.data.server.ErrorTransform;
import com.xudow.app.dynamicstate_old.domain.entity.Dynamic;
import com.xudow.app.dynamicstate_old.domain.entity.DynamicComment;
import com.xudow.app.dynamicstate_old.domain.entity.DynamicFoward;
import com.xudow.app.dynamicstate_old.domain.entity.DynamicLike;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ske on 2016/7/15.
 */
public class DynamicLongPresenter extends BeamListActivityPresenter<DynamicLongActivity, Object> {
    long dynamicId;

    List<DynamicComment> mCommentList = new ArrayList<>();
    List<DynamicFoward> mFowardList = new ArrayList<>();

    public boolean isCommentMode = true;

    public long getDynamicId() {
        return dynamicId;
    }

    @Override
    protected void onCreate(@NonNull DynamicLongActivity view, Bundle savedState) {
        super.onCreate(view, savedState);
        Dynamic dynamic = (Dynamic) getView().getIntent().getSerializableExtra("dynamic");
        if (dynamic != null)
            dynamicId = dynamic.getId();
        else
            dynamicId = getView().getIntent().getLongExtra("id", 0);
        onRefresh();
    }

    Dynamic dynamic;

    @Override
    public void onRefresh() {
        DynamicModel.getInstance().getDynamicDetail(dynamicId)
                .doOnNext(dynamic -> this.dynamic = dynamic)
                .flatMap(dynamic1 -> DynamicModel.getInstance().getLikeListFirst(dynamicId, 10))
                .map(list -> (list == null) ? new ArrayList<DynamicLike>() : list)
                .doOnNext(list -> getAdapter().removeAllHeader())
                .doOnNext(list2 -> getAdapter().addHeader(new RecyclerArrayAdapter.ItemView() {
                    @Override
                    public View onCreateView(ViewGroup parent) {
                        return getView().getHeader(parent, dynamic, list2);
                    }

                    @Override
                    public void onBindView(View headerView) {
                    }
                }))
                .flatMap(list -> DynamicModel.getInstance().getDynamicCommentFirst(dynamicId, 100))
                .doOnNext(listc -> mCommentList = listc)
                .flatMap(list -> DynamicModel.getInstance().getDynamicFowardFirst(dynamicId, 100))
                .doOnNext(listc -> mFowardList = listc)
                .compose(new ErrorTransform<>(ErrorTransform.ServerErrorHandler.TOAST))
                .subscribe(list -> {
                    showData();
                });
    }

    public void showData() {
        getAdapter().clear();
        if (isCommentMode) {
            getAdapter().addAll(mCommentList);
        } else {
            getAdapter().addAll(mFowardList);
        }
    }

    public void showComment() {
        isCommentMode = true;
        showData();
    }

    public void showFoward() {
        isCommentMode = false;
        showData();
    }
}
